<?php
set_time_limit(0);

class GameFlushFinanceAction extends SzAbstractAction
{
    /**
     * 脚本：定时将30天未登入的玩家付费回收
     *
     * @return SzResponse
     */
    public function execute()
    {
        // 处理90天未登入玩家的数据
        $limit = 5;
        $thirtyDaysAgo = SzTime::getTime() - 185 * 24 * 60 * 60;
        $users = array();

        $serversConfigs = SzConfig::get()->loadAppConfig('database', 'MySql');
        foreach ($serversConfigs as $db) {
            $instance = new SzMySqlDb($db);
            $totalCount = $result = $instance->select("SELECT COUNT(`userId`) AS `totalCount` FROM `module_profile`");
            $totalCount = array_shift($totalCount);
            $loopCount = ceil($totalCount['totalCount'] / $limit);

            $start = 0;
            for ($i = 0; $i < $loopCount; $i++) {
                $result = $instance->select("SELECT `userId` FROM `module_profile` WHERE `lastLoginTime` > 0 AND `lastLoginTime` < $thirtyDaysAgo LIMIT $start , $limit");
                $users = array_merge($users, $result);
                $start += $limit;
            }
        }

        // 逐个回收玩家的付费数据
        foreach ($users as $user) {
            $userId = $user['userId'];

            /**
             * @var ModuleProfileVo $user
             */
            $user = SzPersister::get()->getVo($userId, ModuleProfileConst::ORM_NAME_PROFILE);
            $originCommodityValue = $user->getCommodityValue();
            $originPurchasedToken = $user->getPurchasedToken();

            // 清空付费与价值， 付费货币转化为免费货币
            $user->setCommodityValue(0);
            $user->setPurchasedToken(0);
            $user->updateFreeToken($originPurchasedToken);
            SzPersister::get()->setVo($user);

            // 任务进度计数：价值消耗
            if ($originCommodityValue > 0) {
                StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_FINANCE_CASH, array(
                    'clientData' =>array(
                        'tid' => GameConst::ENTITY_ACTION_CONSUME_TOKEN . ':' . abs($originPurchasedToken) . '|',
                        'item' => 2,
                        'value' => abs($originCommodityValue),
                    )
                ));
            }

            /**
             * @var ModulePaymentVo $payment
             */
            $payment = SzPersister::get()->getVo(null, ModulePaymentConst::ORM_NAME_PAYMENT, $userId);
            $originCommodityValue = $payment->getCommodityValue();
            $originPurchasedMoney = $payment->getPurchasedMoney();

            // 清空付费与价值， 付费货币转化为免费货币
            $payment->setCommodityValue(0);
            $payment->setPurchasedMoney(0);
            $payment->updateBonusMoney($originPurchasedMoney);
            SzPersister::get()->setVo($payment);

            // 任务进度计数：价值消耗
            if ($originCommodityValue > 0) {
                StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_FINANCE_CASH, array(
                    'clientData' =>array(
                        'tid' => GameConst::ENTITY_ACTION_CONSUME_MONEY . ':' . abs($originPurchasedMoney) . '|',
                        'item' => 2,
                        'value' => abs($originCommodityValue),
                    )
                ));
            }

            /**
             * @var ModuleItemVoList $itemList
             * @var ModuleItemVo $item
             */
            $itemList = SzPersister::get()->getVoList($userId, ModulePaymentConst::ORM_NAME_PAYMENT);
            foreach ($itemList->getList() as $item) {
                if ($item->getPurchasedCount() > 0) {
                    $originCommodityValue = $item->getCommodityValue();
                    $originPurchasedCount = $item->getPurchasedCount();
                    $item->setCommodityValue(0);
                    $item->setPurchasedCount(0);
                    $item->updateFreeCount($originPurchasedCount);
                    $itemList->updateElement($item);

                    // 任务进度计数：价值消耗
                    if ($originCommodityValue > 0) {
                        $buildEntityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_CONSUME, GameConst::ENTITY_TYPE_ITEM, $item->getItemDefId());
                        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_FINANCE_CASH, array(
                            'clientData' =>array(
                                'tid' => $buildEntityId . ':' . abs($originPurchasedCount) . '|',
                                'item' => 2,
                                'value' => abs($originCommodityValue),
                            )
                        ));
                    }
                }
            }
            SzPersister::get()->setVoList($itemList);
            SzPersister::get()->persist();
        }

        return $this->buildResponse(true);
    }
}